package org.example.utils;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author weigang
 * @create 2022-11-14 16:06
 */
@Slf4j
public class ThreadLocalUtils {
    private static ThreadLocal<String> traceIdThreadLocal = new ThreadLocal();

    private ThreadLocalUtils() {
    }

    public static void setTraceId(String traceId) {
        traceIdThreadLocal.set(traceId);
    }

    public static String getTraceId() {
        if (null == traceIdThreadLocal.get()) {
            traceIdThreadLocal.set(IdUtil.fastSimpleUUID());
        }
        return traceIdThreadLocal.get();
    }

    public static void removeTraceId() {
        traceIdThreadLocal.remove();
    }
}
